package treeviewer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class TreeTest {
	final static Display display = new Display();
	final static Shell shell = new Shell(display);
	final static Tree tree = new Tree(shell, SWT.BORDER | SWT.CHECK);

	static TreeItem root = new TreeItem(tree, SWT.NULL);

	public static void main(String[] args) {
		shell.setText("Tree Sample");
		shell.setLayout(new FillLayout());

		root.setText("��˾");

		initPopup();

		TreeItem child1 = new TreeItem(root, SWT.NULL);
		child1.setText("����");

		TreeItem child2 = new TreeItem(child1, SWT.NULL);
		child2.setText("��λ");

		tree.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				System.out.println(arg0.item.toString());
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				System.out.println(arg0.item.toString());
			}
		});

		shell.setSize(200, 150);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		display.dispose();

	}

	private static void initPopup() {

		Menu menu = new Menu(tree);
		MenuItem newItem = new MenuItem(menu, SWT.PUSH);
		newItem.setText("��������");
		MenuItem newMemberItem = new MenuItem(menu, SWT.PUSH);

		newMemberItem.setText("����Ա��");
		MenuItem editItem = new MenuItem(menu, SWT.PUSH);
		editItem.setText("�༭");
		MenuItem deleteItem = new MenuItem(menu, SWT.PUSH);

		deleteItem.setText("ɾ��");
		tree.setMenu(menu);

	}

}